package cn.edu.sgu.www.mhxysy.service.system.impl;

import cn.edu.sgu.www.mhxysy.dto.system.UserLoginDTO;
import cn.edu.sgu.www.mhxysy.dto.system.UserPassUpdateDTO;
import cn.edu.sgu.www.mhxysy.easyui.component.impl.Editor;
import cn.edu.sgu.www.mhxysy.easyui.component.impl.PropertyGrid;
import cn.edu.sgu.www.mhxysy.entity.system.User;
import cn.edu.sgu.www.mhxysy.feign.clients.pms.PmsFeignService;
import cn.edu.sgu.www.mhxysy.service.system.UserService;
import cn.edu.sgu.www.mhxysy.strategy.login.LoginStrategy;
import cn.edu.sgu.www.mhxysy.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

	private final LoginStrategy loginStrategy;
	private final PmsFeignService feignService;

	@Autowired
	public UserServiceImpl(
			LoginStrategy loginStrategy,
			PmsFeignService feignService) {
		this.loginStrategy = loginStrategy;
		this.feignService = feignService;
	}

	@Override
	public void logout() {
		Subject subject = UserUtils.getSubject();

		if (subject.isAuthenticated()) {
			String username = UserUtils.getLoginUsername();

			log.debug("用户{}退出登录...", username);

			subject.logout();
		}
	}

	@Override
	public void login(UserLoginDTO loginDTO) {
		loginStrategy.setLoginDTO(loginDTO);

		loginStrategy.doLogin();
	}

	@Override
	public void update(User user) {
		String username = UserUtils.getLoginUsername();

		feignService.updateByUsername(user, username);
	}

	@Override
	public void updatePass(UserPassUpdateDTO userPassUpdateDTO) {
		feignService.updatePass(userPassUpdateDTO);
	}

	@Override
	public User getLogin() {
		Subject subject = UserUtils.getSubject();

		if (subject.isAuthenticated()) {
			return UserUtils.getLoginUser();
		}

		return null;
	}

	@Override
	public List<PropertyGrid> selectUserDetails() {
		String group = "用户详情";
		User user = UserUtils.getLoginUser();

		if (user != null) {
			List<PropertyGrid> list = new ArrayList<>();

			/*
			 * 姓名
			 */
			PropertyGrid propertyGrid = new PropertyGrid();

			propertyGrid.setName("姓名");
			propertyGrid.setValue(user.getName());
			propertyGrid.setGroup(group);
			propertyGrid.setEditor(Editor.createEditor("textbox"));

			list.add(propertyGrid);

			/*
			 * 用户名
			 */
			PropertyGrid propertyGrid1 = new PropertyGrid();

			propertyGrid1.setName("用户名");
			propertyGrid1.setValue(user.getUsername());
			propertyGrid1.setGroup(group);
			propertyGrid1.setEditor(Editor.createEditor("textbox"));

			list.add(propertyGrid1);

			/*
			 * 手机号
			 */
			PropertyGrid propertyGrid2 = new PropertyGrid();

			propertyGrid2.setName("手机号");
			propertyGrid2.setValue(user.getPhone());
			propertyGrid2.setGroup(group);
			propertyGrid2.setEditor(Editor.createEditor("numberbox"));

			list.add(propertyGrid2);

			return list;
		}

		return null;
	}

}